package com.example.userlogin.service;

import com.example.userlogin.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 用户审计服务
 * 职责：记录用户操作日志、审计信息
 */
@Service
public class UserAuditService {

    private static final Logger auditLogger = LoggerFactory.getLogger("AUDIT");

    /**
     * 记录用户注册日志
     * 使用新事务，确保即使主事务回滚，审计日志也能保存
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void logUserRegistration(User user) {
        auditLogger.info("USER_REGISTRATION - User: {}, Email: {}, Time: {}", 
            user.getUsername(), user.getEmail(), LocalDateTime.now());
    }

    /**
     * 记录用户登录日志
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void logUserLogin(String username, String ipAddress) {
        auditLogger.info("USER_LOGIN - User: {}, IP: {}, Time: {}", 
            username, ipAddress, LocalDateTime.now());
    }

    /**
     * 记录用户登出日志
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void logUserLogout(String username, String ipAddress) {
        auditLogger.info("USER_LOGOUT - User: {}, IP: {}, Time: {}", 
            username, ipAddress, LocalDateTime.now());
    }

    /**
     * 记录密码修改日志
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void logPasswordChange(String username, String ipAddress) {
        auditLogger.info("PASSWORD_CHANGE - User: {}, IP: {}, Time: {}", 
            username, ipAddress, LocalDateTime.now());
    }

    /**
     * 记录用户信息修改日志
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void logUserUpdate(String username, String field, String oldValue, String newValue) {
        auditLogger.info("USER_UPDATE - User: {}, Field: {}, Old: {}, New: {}, Time: {}", 
            username, field, oldValue, newValue, LocalDateTime.now());
    }

    /**
     * 记录用户删除日志
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void logUserDeletion(String username, String deletedBy) {
        auditLogger.info("USER_DELETION - User: {}, DeletedBy: {}, Time: {}", 
            username, deletedBy, LocalDateTime.now());
    }

    /**
     * 记录认证失败日志
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void logAuthenticationFailure(String username, String ipAddress, String reason) {
        auditLogger.warn("AUTH_FAILURE - User: {}, IP: {}, Reason: {}, Time: {}", 
            username, ipAddress, reason, LocalDateTime.now());
    }

    /**
     * 记录安全事件
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void logSecurityEvent(String eventType, String username, String details) {
        auditLogger.warn("SECURITY_EVENT - Type: {}, User: {}, Details: {}, Time: {}", 
            eventType, username, details, LocalDateTime.now());
    }
}